﻿Imports Models.Solution
Imports System.ComponentModel.DataAnnotations

Public Class Solutions
    Inherits BaseService

    Sub New(request As System.Net.Http.HttpRequestMessage)
        Me.loadToken(request)
    End Sub
    Sub New(token As String)
        Me.token = token
    End Sub

    ''' <summary>
    ''' Gets the specified id.
    ''' </summary>
    ''' <param name="id">The id.</param>
    ''' <returns></returns>
    Public Function [Get](id As Integer) As SolutionVO
        Try
            Me.checkAccess("GETSOLUTION")
            Dim oEntity As SolutionVO

            Using oDB As New Data.BaseModel.BaseModelContainer("name=BaseModelContainer")
                Dim oDataEntity As Data.BaseModel.Solution = (From u In oDB.Solutions Where u.SolutionId = id Select u).FirstOrDefault()

                If oDataEntity IsNot Nothing Then
                    oEntity = New SolutionVO(oDataEntity)
                End If
            End Using

            Return oEntity

        Catch ex As Exception
            MainLib.Helpers.ExceptionWrapper(ex)
        End Try


    End Function

    ' ''' <summary>
    ' ''' Gets the specified login.
    ' ''' </summary>
    ' ''' <param name="login">The id.</param>
    ' ''' <returns></returns>
    'Public Function [Get](login As String) As UserVO
    '    Try
    '        '' Sin permisos y sin controller. Creado para acceder al User en el startup del proyecto FastPath
    '        Dim oEntity As UserVO

    '        Using oDB As New Data.BaseModel.BaseModelContainer("name=BaseModelContainer")
    '            Dim oDataEntity As Data.BaseModel.User = (From u In oDB.Users Where u.UserLogin = login Select u).FirstOrDefault()

    '            If oDataEntity IsNot Nothing Then
    '                oEntity = New UserVO(oDataEntity)
    '            End If
    '        End Using

    '        Return oEntity

    '    Catch ex As Exception
    '        MainLib.Helpers.ExceptionWrapper(ex)
    '    End Try

    'End Function


    ''' <summary>
    ''' Get List of Solutions
    ''' </summary>
    ''' <param name="SolutionId">optional for filter</param>
    ''' <returns>Array of Solutions</returns>
    ''' <remarks></remarks>
    Public Function List(Optional SolutionId As Integer = -1) As IEnumerable(Of SolutionVO)
        Try
            Me.checkAccess("LISTSOLUTIONS")
            Dim oResult As New Generic.List(Of SolutionVO)
            Dim oDataEntity As New Generic.List(Of Data.BaseModel.Solution)

            Using oDB As New Data.BaseModel.BaseModelContainer("name=BaseModelContainer")
                oDataEntity = oDB.Solutions.ToList()
                'If Not SolutionId = -1 Then
                '    Dim oListUserComp As New Generic.List(Of Data.BaseModel.UserSolution)
                '    Dim query As IQueryable(Of Data.BaseModel.UserSolution) = From c In oDB.UserSolutions Where c.SolutionId = SolutionId Select c
                '    oListUserComp = query.ToList()
                '    Dim oDataAux As New Generic.List(Of Data.BaseModel.User)
                '    oDataAux.AddRange(oDataEntity)
                '    For Each oItemUser As Data.BaseModel.User In oDataEntity
                '        For Each oItemUserComp As Data.BaseModel.UserSolution In oListUserComp
                '            If oItemUser.UserId = oItemUserComp.UserId Then
                '                Exit For
                '            ElseIf oItemUserComp Is oListUserComp.Last Then
                '                '' Sino esta en la lista, lo saca
                '                oDataAux.Remove(oItemUser)
                '            End If
                '        Next
                '    Next
                '    oDataEntity.Clear()
                '    oDataEntity.AddRange(oDataAux)
                'End If

                If oDataEntity IsNot Nothing Then
                    For Each dbData As Data.BaseModel.Solution In oDataEntity
                        oResult.Add(New SolutionVO(dbData))
                    Next
                End If

            End Using

            Return oResult

        Catch ex As Exception
            MainLib.Helpers.ExceptionWrapper(ex)
        End Try
    End Function

    ''' <summary>
    ''' Get List of Solutions
    ''' </summary>
    ''' <param name="SolutionId">optional for filter</param>
    ''' <returns>Array of Solutions</returns>
    ''' <remarks></remarks>
    Public Function ListLastEdited(Weeks As Integer, Optional SolutionId As Integer = -1) As IEnumerable(Of SolutionVO)
        Try
            Me.checkAccess("LISTSOLUTIONS")
            Dim oResult As New Generic.List(Of SolutionVO)
            Dim oDataEntity As New Generic.List(Of Data.BaseModel.Solution)

            Using oDB As New Data.BaseModel.BaseModelContainer("name=BaseModelContainer")
                oDataEntity = oDB.Solutions.ToList()
                'If Not SolutionId = -1 Then
                '    Dim oListUserComp As New Generic.List(Of Data.BaseModel.UserSolution)
                '    Dim query As IQueryable(Of Data.BaseModel.UserSolution) = From c In oDB.UserSolutions Where c.SolutionId = SolutionId Select c
                '    oListUserComp = query.ToList()
                '    Dim oDataAux As New Generic.List(Of Data.BaseModel.User)
                '    oDataAux.AddRange(oDataEntity)
                '    For Each oItemUser As Data.BaseModel.User In oDataEntity
                '        For Each oItemUserComp As Data.BaseModel.UserSolution In oListUserComp
                '            If oItemUser.UserId = oItemUserComp.UserId Then
                '                Exit For
                '            ElseIf oItemUserComp Is oListUserComp.Last Then
                '                '' Sino esta en la lista, lo saca
                '                oDataAux.Remove(oItemUser)
                '            End If
                '        Next
                '    Next
                '    oDataEntity.Clear()
                '    oDataEntity.AddRange(oDataAux)
                'End If

                If oDataEntity IsNot Nothing Then
                    For Each dbData As Data.BaseModel.Solution In oDataEntity
                        oResult.Add(New SolutionVO(dbData))
                    Next
                End If

            End Using

            Return oResult

        Catch ex As Exception
            MainLib.Helpers.ExceptionWrapper(ex)
        End Try
    End Function

    ''' <summary>
    ''' Update a solution
    ''' </summary>
    ''' <param name="solution"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Update(solution As SolutionVO) As Integer
        Try
            Me.checkAccess("UPDATESOLUTION")
            Dim nRes As Integer = -1

            Using oDB As New Data.BaseModel.BaseModelContainer("name=BaseModelContainer")

                Dim oDataSolution As Data.BaseModel.Solution = (From c In oDB.Solutions Where c.SolutionId = solution.solutionId Select c).FirstOrDefault()

                If oDataSolution IsNot Nothing Then

                    If Not String.IsNullOrEmpty(solution.name) Then
                        oDataSolution.Name = solution.name
                    End If
                    If Not String.IsNullOrEmpty(solution.description) Then
                        oDataSolution.Description = solution.description
                    End If
                    If Boolean.TryParse(solution.deleted, True) Then
                        oDataSolution.Deleted = solution.deleted
                    End If

                    nRes = oDB.SaveChanges()

                End If
            End Using

            Return nRes

        Catch ex As Exception
            MainLib.Helpers.ExceptionWrapper(ex)
            Return Nothing
        End Try
    End Function

    ''' <summary>
    ''' Create a solution
    ''' </summary>
    ''' <param name="solution"></param>
    ''' <returns>The new ID  of the entity</returns>
    ''' <remarks></remarks>
    Public Function Create(solution As SolutionVO) As Integer
        Try
            Me.checkAccess("CREATESOLUTION")
            Dim nRes As Integer = -1

            Using oDB As New Data.BaseModel.BaseModelContainer("name=BaseModelContainer")

                Dim oDataSolution As New Data.BaseModel.Solution()
                If oDataSolution IsNot Nothing Then
                    If Not String.IsNullOrEmpty(solution.name) Then
                        oDataSolution.Name = solution.name
                    End If
                    If Not String.IsNullOrEmpty(solution.description) Then
                        oDataSolution.Description = solution.description
                    End If
                    'If Not String.IsNullOrEmpty(solution.deleted) Then
                    '    oDataSolution.Deleted = solution.deleted
                    'End If

                    If Boolean.TryParse(solution.deleted, True) Then
                        oDataSolution.Deleted = solution.deleted
                    End If

                    oDB.Solutions.AddObject(oDataSolution)
                End If
                If oDB.SaveChanges() > 0 Then
                    nRes = oDataSolution.SolutionId
                End If
            End Using

            Return nRes

        Catch ex As Exception
            MainLib.Helpers.ExceptionWrapper(ex)
            Return Nothing
        End Try

    End Function

    ''' <summary>
    ''' Delete a Solution
    ''' </summary>
    ''' <param name="id">SolutionId</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Delete(id As Integer) As Integer
        Try
            Me.checkAccess("DELETESOLUTION")
            Dim nRes As Integer = -1

            Using oDB As New Data.BaseModel.BaseModelContainer("name=BaseModelContainer")

                Dim oData As Data.BaseModel.Solution = (From c In oDB.Solutions Where c.SolutionId = id Select c).FirstOrDefault()

                If oData IsNot Nothing Then
                    oDB.DeleteObject(oData)
                    nRes = oDB.SaveChanges()
                End If

            End Using

            Return nRes

        Catch ex As Exception
            MainLib.Helpers.ExceptionWrapper(ex)
            Return Nothing
        End Try

    End Function

End Class